# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
_base_ver=2.4.0
pkgver=2.4.0.16752.6eb10ef
pkgrel=1
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url=""
license=('GPL')
groups=('base')
depends=()
makedepends=('cocom'
             'git'
             'perl'
             'gcc'
             'make'
             'mingw-w64-cross-gcc'
             'mingw-w64-cross-zlib'
             'zlib-devel'
             'gettext-devel'
             'libiconv-devel'
             'diffutils')
# options=('debug' '!strip')
source=('msys2-runtime'::'git+https://github.com/Alexpux/Cygwin.git#branch=msys2-master'
        '0001-dcrt0.cc-Untangle-allow_glob-from-winshell.patch'
        '0002-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch'
        '0003-strace.cc-Don-t-set-MSYS-noglob.patch'
        '0004-Add-debugging-for-build_argv.patch'
        '0005-Add-debugging-for-strace-make_command_line.patch')
md5sums=('SKIP'
         '6b959c674a687709d93fb562143c2cbc'
         '8666904817991a621b64e3382a53f452'
         '923b7800812f2654c395e8c07ad404bd'
         '5055fa5115d611d8501989d0e345199e'
         '77b4c20306527ad2e5a8845c4c37e386')

pkgver() {
  cd "${srcdir}"/msys2-runtime
  #local gitver=$(git describe --tags | sed 's|cygwin-||g' | sed 's|_|.|g')
  #gitver=${gitver//-*}
  printf "%s.%s.%s" "${_base_ver}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}"/msys2-runtime
  git am "${srcdir}"/0001-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
  git am "${srcdir}"/0002-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
  git am "${srcdir}"/0003-strace.cc-Don-t-set-MSYS-noglob.patch
  git am "${srcdir}"/0004-Add-debugging-for-build_argv.patch
  git am "${srcdir}"/0005-Add-debugging-for-strace-make_command_line.patch
}

build() {
  [[ -d "${srcdir}"/build-${CHOST} ]] && rm -rf "${srcdir}"/build-${CHOST}
  mkdir -p "${srcdir}"/build-${CHOST} && cd "${srcdir}"/build-${CHOST}

  # Gives more verbose compile output when debugging.
  local -a extra_config
  if check_option "debug" "y"; then
    export CCWRAP_VERBOSE=1
    OPTIM="-O0"
    extra_config+=(--enable-debugging)
  else
    OPTIM="-O2"
  fi

  CFLAGS="$OPTIM -pipe -ggdb"
  CXXFLAGS="$OPTIM -pipe -ggdb"

  "${srcdir}"/msys2-runtime/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    "${extra_config[@]}"
  LC_ALL=C make
  LC_ALL=C make -j1 DESTDIR="${srcdir}"/dest install

  #pushd ${CHOST}/winsup/cygwin > /dev/null
  #LANG=C make libmsys2_s.a
  #cp libmsys2_s.a "${srcdir}"/dest/usr/${CHOST}/lib/
  #popd > /dev/null

  rm -rf "${srcdir}"/dest/etc
}

package_msys2-runtime() {
  pkgdesc="Posix emulation engine for Windows"
  groups=('base')
  options=('!strip')
  #install=msys2-runtime.install

  mkdir -p "${pkgdir}"/usr
  cp -rf "${srcdir}"/dest/usr/bin "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/bin/msys-2.0.dbg
  rm -f "${pkgdir}"/usr/bin/cyglsa-config
  rm -f "${pkgdir}"/usr/bin/cyglsa.dll
  rm -f "${pkgdir}"/usr/bin/cyglsa64.dll
  rm -f "${pkgdir}"/usr/bin/cygserver-config
  cp -rf "${srcdir}"/dest/usr/share "${pkgdir}"/usr/
}

package_msys2-runtime-devel() {
  pkgdesc="MSYS2 headers and libraries"
  groups=('MSYS2-devel')
  depends=("msys2-runtime=${pkgver}")
  options=('staticlibs' '!strip')

  mkdir -p "${pkgdir}"/usr/bin
  cp -f "${srcdir}"/dest/usr/bin/msys-2.0.dbg "${pkgdir}"/usr/bin/
  cp -rLf "${srcdir}"/dest/usr/${CHOST}/include "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/include/iconv.h
  rm -f "${pkgdir}"/usr/include/unctrl.h
  # provided by libtirpc
  rm -fr "${pkgdir}"/usr/include/rpc/

  cp -rLf "${srcdir}"/dest/usr/${CHOST}/lib "${pkgdir}"/usr/
}

# return 0
# To hack on this:
# cd /c/repo-MSYS2/msys2-runtime/
# pushd src/build-i686-pc-msys
# LANG=C make && make -j1 DESTDIR=/c/repo-MSYS2/msys2-runtime/src/dest install
# popd
# makepkg -sRLf
# pacman -U msys2-runtime*.xz

# Quicker:
# open cmd.exe
# set "PATH=C:\\msys32\\usr\\bin;%PATH%"
# E:
# pushd E:\m2\repo-MSYS2\msys2-runtime\src\build-i686-pc-msys\i686-pc-msys\winsup\cygwin
# C:/msys32/usr/bin/bash -c "LANG=C && make"
# copy /y new-msys-2.0.dll C:\msys32\usr\bin\msys-2.0.dll
# popd
# C:
# C:/msys32/usr/bin/strace ls / > C:/strace.txt 2>&1
#
